package cn.ms.neural.moduler.passrate.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.ms.neural.common.exception.PassRateException;
import cn.ms.neural.entity.NeuralConf;
import cn.ms.neural.moduler.passrate.IPassRate;
import lombok.Getter;

public class PassRateFactory implements IPassRate {

    private NeuralConf conf;
    /**
     * 放通率计算
     */
    @Getter
    private Random passRateRandom = new Random();

    public PassRateFactory(NeuralConf conf) {
        this.conf = conf;
    }

    public boolean isPassRateEnable() {
        return conf.isPassRateEnable();
    }

    public void passRate() throws Throwable {
        if (!isPassRateEnable()) {//开关未开,则跳过校验器
            return;
        }
        if (passRateRandom.nextDouble() > conf.getPassRate()) {//放通率控制
            throw new PassRateException(
                    "The rate of pass through rate is " + "declined, the current rate(passRate) is " + (conf
                            .getPassRate() * 100) + "%.");
        }
    }

    public static void main(String[] args) {
        List<Double> over = new ArrayList<>();
        List<Double> down = new ArrayList<>();
        PassRateFactory passRate = new PassRateFactory(null);

        int size = 10000000;
        for (int i = 0 ; i < size; i ++){
            double d = passRate.getPassRateRandom().nextDouble();
            if (d < 0.5D){
                down.add(d);
            }else {
                over.add(d);
            }
        }
        System.out.println("over: " + over.size());
        System.out.println("down: " + down.size());
    }
}
